﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 网上考勤人员定义
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AttendanceDefinedController : Controller
    {

        /// <summary>
        /// 人员逻辑类
        /// </summary>
        private BLL.HREmpl hrEmplBLL = new BLL.HREmpl();
        //
        // GET: /Admin/AttendanceDefined/
        [AuthorityActionFilter("m_AttendanceDefined", Order = 1)]
        [LogPageActionFilter("考勤人员定义", Order = 2)]
        public ActionResult Index()
        {
            ViewData["tableJsonStr"] = GetTableJsonStr();
            string strTable = ViewData["tableJsonStr"].ToString();

            ViewData["strTable"] = GetEmpTable(strTable);

            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public string GetTableJsonStr(string returnValue = "") 
        {
            string tableJsonStr = "";
            string entityNo = GlobalConstant.ENTITYNO;  
            BLL.AttendanceDefined attendanceDefinedBLL = new BLL.AttendanceDefined();
            DataTable dt = attendanceDefinedBLL.GetListByEntityNo(entityNo);

            tableJsonStr += "[";
            if (returnValue == "")
            {
                if (dt.Rows.Count > 0) 
                {
                    foreach (DataRow dr in dt.Rows) 
                    {
                        tableJsonStr += "{";
                        tableJsonStr += "\"FEmpName\":\"" + dr["FEmpName"].ToString() + "\",\"FEmpNo\":\"" + dr["FEmplNo"].ToString() + "\",\"FDeptName\":\"" + dr["deptName"].ToString() + "\",\"FDeptNo\":\"" + dr["FDeptNo"].ToString() + "\",\"Value\":\"" + dr["FEmplNo"].ToString() + "\"";
                        tableJsonStr += "},";
                    }
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            else
            {
                if (dt.Rows.Count > 0)
                {
                    List<Model.HREmpl> emplModelList1 = new List<Model.HREmpl>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        Model.HREmpl emplModel = new Model.HREmpl();
                        emplModel.FEmplNo = dr["FEmplNo"].ToString();
                        emplModel.FEmpName = dr["FEmpName"].ToString();
                        emplModel.FEntityNo = entityNo;
                        emplModel.FIsAttendance = "N";
                        emplModelList1.Add(emplModel);
                    }
                    try
                    {
                        attendanceDefinedBLL.UpdataEmpl(emplModelList1);
                    }
                    catch (Exception)
                    { }
                }

                List<SelectListItem> itemList = JsonConvert.DeserializeObject<List<SelectListItem>>(returnValue);
                List<Model.HREmpl> emplModelList = new List<Model.HREmpl>();

                foreach (SelectListItem selectItem in itemList)
                {
                    Model.HREmpl emplModel = new Model.HREmpl();
                    emplModel.FEmplNo = selectItem.Value;
                    emplModel.FEmpName = selectItem.Text;
                    emplModel.FEntityNo = entityNo;
                    emplModel.FIsAttendance = "Y";
                    emplModelList.Add(emplModel);
                    tableJsonStr += hrEmplBLL.GetEmpNameAndDept(entityNo, selectItem.Value) + ",";
                }
                //修考核人员参加网上考勤
                try 
                {
                    attendanceDefinedBLL.UpdataEmpl(emplModelList);
                }
                catch (Exception) 
                { }

                if (itemList.Count > 0)
                {
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            tableJsonStr += "]";
            return tableJsonStr;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetEmpTable(string strTable) 
        {
            string str = "";
            string deptNo = "";
            int td = 8;//一行显示的人员个数
            int ss = 0;//每个部门的总人数
            int tr = 0;
            List<Model.HREmpl> itemList = JsonConvert.DeserializeObject<List<Model.HREmpl>>(strTable);
            List<int> intList = new List<int>();
            
            for (int i = 0; i < itemList.Count; i++)
            {
                if (deptNo != itemList[i].FDeptNo)
                {
                    deptNo = itemList[i].FDeptNo;
                    if (ss != 0) 
                    {
                        intList.Add(ss);
                    }
                    ss = 0;
                    ss++;
                    tr++;
                }
                else
                {
                    ss++;
                }              
            }
            intList.Add(ss);

            List<int> trIntList = new List<int>();
            
            foreach (int p in intList)
            {
                int a = 0;
                a = p;
                if (a % td == 0)
                {
                    a = a / td;
                }
                else 
                {
                    a = a / td + 1;
                }
                trIntList.Add(a);
            }


            deptNo = "";
            str += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"99%\" class=\"table\">";
            str += "<tr><td style=\"font-size:14px;font-weight:bold;text-align:center;width:100px;\">部门</td><td style=\"font-size:14px;font-weight:bold;text-align:center\" colspan=\"" + td + "\">人员</td></tr>";
            int j = 0;
            int sum = 0;
            int count = 0;
            foreach (Model.HREmpl model in itemList) 
            {
                if (deptNo != model.FDeptNo)
                {
                    deptNo = model.FDeptNo;

                    if (j > 0) 
                    {
                        if (sum < td) 
                        {
                            for (int i = 0; i < td - sum; i++)
                            {
                                str += "<td>&nbsp;</td>";
                            }
                        }
                        str += "</tr>";
                    }

                    str += "<tr>";
                    str += "<td style=\"font-size:12px;font-weight:bold;text-align:center;\" rowspan=\"" + trIntList[j] + "\">" + model.FDeptName + "</td>";
                    str += "<td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";

                    j++;
                    sum = 0;
                    sum++;
                }
                else 
                {
                    if (sum >= td)
                    {
                        str += "</tr><tr><td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";
                        //int abs = Math.Abs(sum - td - td) + 1;
                        //for (int i = 0; i < abs; i++)
                        //{
                        //    str += "<td class=\"tdClass\" width=\"50\">&nbsp;</td>";
                        //}
                        sum = 0;
                        count++;
                    }
                    else 
                    {
                        str += "<td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";
                    }
                    sum++;
                }
            }
            //str += "</tr>";
            //int abs1 = Math.Abs(sum - td) + 2;
            //for (int i = 0; i < abs1; i++)
            //{
            //    str += "<td class=\"tdClass\" width=\"50\">&nbsp;</td>";
            //}
            //if (ss > 0)
            //{
            //    str += "</tr>";
            //}
            if (sum < td)
            {
                for (int i = 0; i < td - sum; i++)
                {
                    str += "<td>&nbsp;</td>";
                }
            }
            str += "</tr>";
            str += "</table>";
            return str;
        }


    }
}
